﻿using TextToHTML;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using TextToHTMLExtensionInterfaces;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using System.ComponentModel.Composition.Primitives;

namespace t_TextToHTML
{
    
    
    /// <summary>
    ///This is a test class for RenderHTMLToolsTest and is intended
    ///to contain all RenderHTMLToolsTest Unit Tests
    ///</summary>
    [TestClass()]
    public class RenderHTMLToolsTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        private CompositionContainer _container { get; set; }

        [TestInitialize()]
        public void MyTestInitialize()
        {
            var catalog = new AggregateCatalog();

            _container = new CompositionContainer(catalog);
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetAssembly(typeof(TextToHTML.RenderHTMLTools))));
        }

        [TestCleanup()]
        public void MyTestCleanup()
        {
            _container.Dispose();
        }

        /// <summary>
        /// Make sure we are generating a file when we do this (i.e. the simple stuff just
        /// works!
        ///</summary>
        [TestMethod()]
        public void RenderTextTest()
        {
            RenderHTMLTools_Accessor rh = new RenderHTMLTools_Accessor();
            FileInfo actual = rh.RenderText("hi");
            Assert.IsTrue(actual.Exists);
        }

        /// <summary>
        /// Null Line
        ///</summary>
        [TestMethod()]
        [DeploymentItem("TextToHTML.exe")]
        public void RenderTextToStreamNullLine()
        {
            StringWriter wr = new StringWriter();
            RenderHTMLTools_Accessor rh = new RenderHTMLTools_Accessor();
            rh.RenderTextToStream(wr, "");
            Assert.AreEqual("", wr.ToString());
        }

        /// <summary>
        /// Simple, single, line.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("TextToHTML.exe")]
        public void RenderTextToStreamTest()
        {
            StringWriter wr = new StringWriter();
            RenderHTMLTools_Accessor rh = new RenderHTMLTools_Accessor();
            rh.RenderTextToStream(wr, "single line");
            Assert.AreEqual("<p>single line</p>", wr.ToString());
        }

        /// <summary>
        /// Two lines.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("TextToHTML.exe")]
        public void RenderTextToStreamTwoLineTest()
        {
            StringWriter sourceStuff = new StringWriter();
            sourceStuff.WriteLine("line1");
            sourceStuff.WriteLine("line2");

            StringWriter wr = new StringWriter();
            RenderHTMLTools_Accessor rh = new RenderHTMLTools_Accessor();
            rh.RenderTextToStream(wr, sourceStuff.ToString());

            StringWriter expectedStuff = new StringWriter();
            expectedStuff.WriteLine("<p>line1</p>");
            expectedStuff.Write("<p>line2</p>");
            Assert.AreEqual(expectedStuff.ToString(), wr.ToString());
        }

        /// <summary>
        /// Three lines - with a blank line.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("TextToHTML.exe")]
        public void RenderTextToStreamThreeLineTestWithABlank()
        {
            StringWriter sourceStuff = new StringWriter();
            sourceStuff.WriteLine("line1");
            sourceStuff.WriteLine("");
            sourceStuff.WriteLine("line2");

            StringWriter wr = new StringWriter();
            RenderHTMLTools_Accessor rh = new RenderHTMLTools_Accessor();
            rh.RenderTextToStream(wr, sourceStuff.ToString());

            StringWriter expectedStuff = new StringWriter();
            expectedStuff.WriteLine("<p>line1</p>");
            expectedStuff.WriteLine("<p></p>");
            expectedStuff.Write("<p>line2</p>");
            Assert.AreEqual(expectedStuff.ToString(), wr.ToString());
        }

        /// <summary>
        /// Simple picture http
        ///</summary>
        [TestMethod()]
        [DeploymentItem("TextToHTML.exe")]
        public void RenderTextToStreamPNGHttpLinK()
        {
            StringWriter wr = new StringWriter();
            RenderHTMLTools_Accessor rh = new RenderHTMLTools_Accessor(_container);
            rh.RenderTextToStream(wr, "http://mppmu.mpg.de/~pataraia/JetwithNoTrack.png");

            Assert.AreEqual("<p><img src=\"http://mppmu.mpg.de/~pataraia/JetwithNoTrack.png\" /></p>", wr.ToString());
        }

        /// <summary>
        ///A test a single line of text
        ///</summary>
        [TestMethod()]
        [DeploymentItem("TextToHTML.exe")]
        public void RenderTextLineToStringSingleLine()
        {
            RenderHTMLTools_Accessor rh = new RenderHTMLTools_Accessor();
            Assert.AreEqual("hi", rh.RenderTextLineToString("hi"));
        }

        /// <summary>
        ///A test a blank line of text
        ///</summary>
        [TestMethod()]
        [DeploymentItem("TextToHTML.exe")]
        public void RenderTextLineToStringBlankLine()
        {
            RenderHTMLTools_Accessor rh = new RenderHTMLTools_Accessor();
            Assert.AreEqual("", rh.RenderTextLineToString(""));
        }

        /// <summary>
        ///http only
        ///</summary>
        [TestMethod()]
        [DeploymentItem("TextToHTML.exe")]
        public void RenderTextLineToStringhttponlyImage()
        {
            RenderHTMLTools_Accessor rh = new RenderHTMLTools_Accessor(_container);
            Assert.AreEqual("<img src=\"http://mppmu.mpg.de/~pataraia/JetwithNoTrack.png\" />", rh.RenderTextLineToString("http://mppmu.mpg.de/~pataraia/JetwithNoTrack.png"));
        }

        /// <summary>
        ///http in text
        ///</summary>
        [TestMethod()]
        [DeploymentItem("TextToHTML.exe")]
        public void RenderTextLineToStringhttpwithTextImage()
        {
            RenderHTMLTools_Accessor rh = new RenderHTMLTools_Accessor(_container);
            Assert.AreEqual("hi <img src=\"http://mppmu.mpg.de/~pataraia/JetwithNoTrack.png\" /> there", rh.RenderTextLineToString("hi http://mppmu.mpg.de/~pataraia/JetwithNoTrack.png there"));
        }

        /// <summary>
        ///A test for Overlaps
        ///</summary>
        [TestMethod()]
        [DeploymentItem("TextToHTML.exe")]
        public void OverlapsTest()
        {
            RenderHTMLTools_Accessor rh = new RenderHTMLTools_Accessor();
            Assert.IsFalse(rh.Overlaps(new TextRun { Start = 1, Length = 1 }, new TextRun { Start = 10, Length = 5 }));
            Assert.IsFalse(rh.Overlaps(new TextRun { Start = 10, Length = 5 }, new TextRun { Start = 1, Length = 1 }));
            Assert.IsTrue(rh.Overlaps(new TextRun { Start = 1, Length = 50 }, new TextRun { Start = 10, Length = 5 }));
            Assert.IsTrue(rh.Overlaps(new TextRun { Start = 10, Length = 5 }, new TextRun { Start = 1, Length = 50 }));
        }
    }
}
